package com.atguigu.proxy.proxy;

import com.atguigu.proxy.api.Calculator;

/**
 * 静态代理：
 *      能够初步实现核心业务和非核心业务的剥离、解耦
 *      但是代理的逻辑是硬编码的，不能够灵活的扩展，目前只能针对 Calculator 这一个接口做代理
 */
public class StaticProxy implements Calculator {

    private Calculator target;

    public StaticProxy(Calculator target) {
        this.target = target;
    }

    @Override
    public int add(int i, int j) {
        try {
            System.out.println("add方法开始了，入参列表是：" + i + "," + j);

            int result = target.add(i, j);

            System.out.println("add方法结束了，返回值是：" + result);

            return result;
        } catch (Exception e) {
            System.out.println("add方法异常了，异常类型是：" + e.getClass().getName());
            throw new RuntimeException(e);
        } finally {
            System.out.println("add方法结束了");
        }
    }

    @Override
    public int sub(int i, int j) {
        try {
            System.out.println("sub方法开始了，入参列表是：" + i + "," + j);

            int result = target.sub(i, j);

            System.out.println("sub方法结束了，返回值是：" + result);

            return result;
        } catch (Exception e) {
            System.out.println("sub方法异常了，异常类型是：" + e.getClass().getName());
            throw new RuntimeException(e);
        } finally {
            System.out.println("sub方法结束了");
        }
    }

    @Override
    public int mul(int i, int j) {
        try {
            System.out.println("mul方法开始了，入参列表是：" + i + "," + j);

            int result = target.mul(i, j);

            System.out.println("mul方法结束了，返回值是：" + result);

            return result;
        } catch (Exception e) {
            System.out.println("mul方法异常了，异常类型是：" + e.getClass().getName());
            throw new RuntimeException(e);
        } finally {
            System.out.println("mul方法结束了");
        }
    }

    @Override
    public int div(int i, int j) {
        try {
            System.out.println("div方法开始了，入参列表是：" + i + "," + j);

            int result = target.div(i, j);

            System.out.println("div方法结束了，返回值是：" + result);

            return result;
        } catch (Exception e) {
            System.out.println("div方法异常了，异常类型是：" + e.getClass().getName());
            throw new RuntimeException(e);
        } finally {
            System.out.println("div方法结束了");
        }
    }
}
